package com.fiberhome.ztelauncher.uilib;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.util.TextStyleUtil;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataCommon;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by xc on 2018/3/20.
 * 描述：
 */

public class DateLayout extends LinearLayout {


    private TextView tvHM;
    private TextView tvDW;
    private long t;
	private View viewDivide;

    /**
     *
     * @param context
     */
    public DateLayout(Context context) {
        super(context);
        initView(context);
    }

    /**
     *
     * @param context
     * @param attrs
     */
    public DateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public DateLayout(Context context,  AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_date, this, true);
        tvHM = (TextView) view.findViewById(R.id.tv_hour_min);
        tvDW = (TextView) view.findViewById(R.id.tv_date_week);
        viewDivide = (View) view.findViewById(R.id.view);
    }

    /**
     *
     * @param navs
     */
    public void setTextStyle(LaunchDataCommon.Navs navs) {
        tvHM.setTextColor(Color.parseColor(navs.fontColor));
        tvHM.setTextSize(TypedValue.COMPLEX_UNIT_DIP, navs.fontSize);
        tvHM.getPaint().setTypeface(TextStyleUtil.getTextStyle(navs.fontStyle));
        tvDW.setTextColor(Color.parseColor(navs.fontColor));
        tvDW.setTextSize(TypedValue.COMPLEX_UNIT_DIP, (int) (navs.fontSize / 2));
        tvDW.getPaint().setTypeface(TextStyleUtil.getTextStyle(navs.fontStyle));
        LayoutParams lp = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.width = 2;
        lp.height = navs.fontSize * 2;
        lp.leftMargin = 2;
        lp.rightMargin = 2;
        viewDivide.setLayoutParams(lp);
    }

    /**
     *
     * @param time
     */
    public void setDate(long time) {
        t = time;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");// HH:mm:ss
        Date date = new Date(time);
        String strDate = sdf.format(date);
        setTime(strDate.substring(6));

        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int week = mCalendar.get(Calendar.DAY_OF_WEEK);
        tvDW.setText(strDate.substring(0, 5) + "\n" + getWeek(week));

        runTime();
    }

    private void setTime(String strTime) {
        int hour = Integer.valueOf(strTime.substring(0,2));
        int min = Integer.valueOf(strTime.substring(3));
        if (hour <= 12) {
            tvHM.setText(strTime + "AM");
        } else {
            tvHM.setText(+ hour + ":" + min + "PM");
        }
    }

    /**
     * 时钟计时
     */
    private void runTime() {
        tvHM.postDelayed(new Runnable() {
            @Override
            public void run() {
                t += 30000;
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String strTime = sdf.format(new Date(t));
                setTime(strTime);
                tvHM.postDelayed(this, 30 * 1000);
            }
        }, 30 * 1000);
    }

    private String getWeek(int week) {
        String strWeek = null;
        switch (week) {
            case Calendar.SUNDAY:
                strWeek = "SUN";
                break;
            case Calendar.MONDAY:
                strWeek = "MON";
                break;
            case Calendar.TUESDAY:
                strWeek = "TUE";
                break;
            case Calendar.WEDNESDAY:
                strWeek = "WED";
                break;
            case Calendar.THURSDAY:
                strWeek = "THU";
                break;
            case Calendar.FRIDAY:
                strWeek = "FRI";
                break;
            case Calendar.SATURDAY:
                strWeek = "SAT";
                break;
            default:
                break;
        }
        return strWeek;
    }

    public boolean hasEmpty() {
        return TextUtils.isEmpty(tvHM.getText().toString()) && TextUtils.isEmpty(tvDW.getText().toString());
    }
}
